package Control;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

import Entity.Workshop_TrainerEntity;
import Entity.Workshop_WorkshopCategory;
import Entity.Workshop_WorkshopEntity;
import Entity.Workshop_WorkshopStatusCode;
import Form.Workshop_CreateWorkshop_Form;

public class Workshop_CreateWorkshop_Controller {
	//Object Attributes
	private Workshop_WorkshopEntity newWorkshop = new Workshop_WorkshopEntity();
	private Workshop_WorkshopCategory selectedCategory = new Workshop_WorkshopCategory();
	private Workshop_TrainerEntity selectedTrainer = new Workshop_TrainerEntity();
	private Workshop_WorkshopStatusCode statusCode = new Workshop_WorkshopStatusCode("AVA");
	ArrayList<Workshop_WorkshopCategory> categoryList = new ArrayList<Workshop_WorkshopCategory>();
	ArrayList<Workshop_TrainerEntity> trainerList = new ArrayList<Workshop_TrainerEntity>();

	private Workshop_CreateWorkshop_Form parser;
	
	public Workshop_CreateWorkshop_Form getParser() {
		return parser;
	}

	public void setParser(Workshop_CreateWorkshop_Form parser) {
		this.parser = parser;
	}

	public Workshop_CreateWorkshop_Controller(final Workshop_CreateWorkshop_Form parser){
		this.setParser(parser);
		
		SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>(){

			@Override
			protected Void doInBackground() throws Exception {
				newWorkshop.setWorkshopId(CommonControls.generateDefaultType5DigitCode('W'));
				categoryList = Workshop_WorkshopCategory.retrieveAllCategory();
				trainerList = Workshop_TrainerEntity.retrieveAllTrainers();
				
				return null;
			}
			
			protected void done(){
				getParser().getLblWorkshopId().setText(newWorkshop.getWorkshopId());
				
				getParser().getCategory().addItem("Select a category..");
				for(int i = 0; i < categoryList.size(); i++){
					getParser().getCategory().addItem(categoryList.get(i).getDescription());
				}
				
				getParser().getTrainerNameCombo().addItem("Select a trainer..");
				for(int i = 0; i < trainerList.size(); i++){
					getParser().getTrainerNameCombo().addItem(trainerList.get(i).getTrainerName());
				}

				CommonControls.togglePanelModality(parser);
				parser.getParser().getParser().closeProgressBar();
			}
		};
		CommonControls.togglePanelModality(parser);
		parser.getParser().getParser().getProgressBar();
		worker.execute();
	}
	
	public void setTrainer(){
		if(getParser().getTrainerNameCombo().getSelectedIndex() > 0){
			selectedTrainer = trainerList.get(getParser().getTrainerNameCombo().getSelectedIndex()-1);
			getParser().getTrainerName().setText(selectedTrainer.getTrainerName());
			getParser().getContactNo().setText(Integer.toString(selectedTrainer.getContactNo()));
		}
		else{
			getParser().getTrainerName().setText("");
			getParser().getContactNo().setText("");
		}
	}

	public void setTrainerDetails(){
		if(getParser().getNewTrainer().isSelected()){
			getParser().getTrainerNameCombo().setEnabled(false);
			getParser().getTrainerName().setText("");
			getParser().getTrainerName().setEditable(true);
			getParser().getContactNo().setText("");
			getParser().getContactNo().setEditable(true);
		}
		else{
			getParser().getTrainerNameCombo().setEnabled(true);
			getParser().getTrainerName().setEditable(false);
			getParser().getContactNo().setEditable(false);
			setTrainer();
		}
	}
	
	public void createWorkshop(){
		//Validation
		boolean catValid = true, trainer = true;
		
		//Validate category
		if(getParser().getCategory().getSelectedIndex() == 0){
			getParser().getCategory().setBorder(BorderFactory.createLineBorder(Color.RED, 2));
			catValid = false;
		}
		else
			getParser().getCategory().setBorder(null);
		
		//Validate trainer
		if(!getParser().getNewTrainer().isSelected()){
			if(getParser().getTrainerNameCombo().getSelectedIndex() == 0){
				getParser().getTrainerNameCombo().setBorder(BorderFactory.createLineBorder(Color.RED, 2));
				trainer = false;
			}
			else
				getParser().getTrainerNameCombo().setBorder(null);
		}
		else
			getParser().getTrainerNameCombo().setBorder(null);
		
		if(CommonControls.validatePanelFields(parser) && catValid && trainer){
			if(getParser().getContactNo().getText().length() == 8){
			
				if(getParser().getCategory().getSelectedIndex() > 0){
					selectedCategory = categoryList.get(getParser().getCategory().getSelectedIndex()-1);
				}
				
				newWorkshop.setCategory(selectedCategory);
				newWorkshop.setWorkshopName(getParser().getWorkshopName().getText());
				newWorkshop.setDescription(getParser().getDescription().getText());
				newWorkshop.setVenue(getParser().getVenue().getText());
				newWorkshop.setStartDateTime((Date)(getParser().getStartDateTime().getValue()));
				newWorkshop.setEndDateTime((Date)(getParser().getEndDateTime().getValue()));
				newWorkshop.setWorkshopDateTime(CommonControls.getCurrentDate());
				newWorkshop.setCapacity(Integer.valueOf(getParser().getCapacity().getValue().toString()));
				if(getParser().getNewTrainer().isSelected()){
					Workshop_TrainerEntity newTrainer = new Workshop_TrainerEntity();
					newTrainer.setTrainerId(CommonControls.generateDefaultType5DigitCode('T'));
					newTrainer.setTrainerName(getParser().getTrainerName().getText());
					newTrainer.setContactNo(Integer.parseInt(getParser().getContactNo().getText()));
					newTrainer.createTrainer();
					newWorkshop.setTrainer(newTrainer);
				}
				else{
					newWorkshop.setTrainer(selectedTrainer);
				}
				newWorkshop.setStatusCode(statusCode);
				newWorkshop.createWorkshop();
				JOptionPane.showMessageDialog(null, "Workshop successfully created");
				getParser().getParser().getCreate().dispose();
				getParser().getParser().getControl().generateList();
			}
		}
		else if(getParser().getContactNo().getText().length() != 8)
			getParser().getContactNo().setBorder(BorderFactory.createLineBorder(Color.RED, 2));
	}
}
